# -.- coding: utf-8 -.-

from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse

from app_planning.models import *
from app_planning.forms import *

###############################
# PLANNINGS
###############################
def new_planning(request, template='app_planning/new_form.html', extra_context={}):
    if request.method == 'POST':
        extra_context['form'] = PlanningForm(request.POST)
        if extra_context['form'].is_valid():
            planning = extra_context['form'].save()
            return HttpResponseRedirect(reverse('view_planning', args=[planning.pk]))
    else:
        extra_context['form'] = PlanningForm()
    
    extra_context['form_description'] = "app_planning/descriptions/planning.html"
    extra_context['form_action'] = reverse('new_planning')
    return render_to_response(template, extra_context, context_instance=RequestContext(request))

def view_planning(request, pk, template='app_planning/view_planning.html', extra_context={}):
    extra_context['planning'] = Planning.objects.get(id=pk)
    extra_context['days'] = ("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo")
    extra_context['initial_hour'] = 7
    extra_context['last_hour'] = 23
    extra_context['time_intervals'] = []
    for i in range(extra_context['initial_hour'], extra_context['last_hour']):
        extra_context['time_intervals'].append(str(i)+":00")
        extra_context['time_intervals'].append(str(i)+":30")
        
    return render_to_response(template, extra_context, context_instance=RequestContext(request))

def delete_planning(request, planning_id, template='', extra_context={}):
    try:
        plan = Planning.objects.get(pk=planning_id)
        plan.delete()
    except Planning.DoesNotExist:
        pass
    
    return HttpResponseRedirect(reverse('list_planning'))

# Establece un planning como activo
# Si había algún otro previamente activo, lo desactiva (sólo puede haber uno)
def activate(request, planning_id, template='', extra_context={}):
    try:
        old_active = Planning.objects.get(active=True)
        old_active.active = False
        old_active.save()            
    except Planning.DoesNotExist:
        pass
        
    new_active = get_object_or_404(Planning, pk=planning_id)
    new_active.active = True
    new_active.save()
    
    return HttpResponseRedirect(reverse('list_planning'))

###############################
# ACTIONS
###############################
def new_action(request, planning_id, template='app_planning/new_form.html', extra_context={}):
    extra_context['planning'] = get_object_or_404(Planning, pk=planning_id)
    if request.method == 'POST':
        extra_context['form'] = ActionForm(request.POST)
        if extra_context['form'].is_valid():
            action = extra_context['form'].save(commit=False)
            action.planning = extra_context['planning']
            action.save()
            return HttpResponseRedirect(reverse('view_planning', args=[extra_context['planning'].pk]))
    else:
        extra_context['form'] = ActionForm()
        
    extra_context['form_description'] = "app_planning/descriptions/action.html"
    extra_context['form_action'] = reverse('new_action', args=[extra_context['planning'].pk])
    return render_to_response(template, extra_context, context_instance=RequestContext(request))

def delete_action(request, action_id, template='', extra_context={}):
    try:
        action = Action.objects.get(id=action_id)
        action.delete()
    except:
        pass
    
    return HttpResponseRedirect(reverse('view_planning', args=[action.planning_id]))

###############################
# TIMESLOTS
###############################
def new_timeslot(request, action_id, template='app_planning/new_form.html', extra_context={}):
    extra_context['action'] = get_object_or_404(Action, pk=action_id)
    if request.method == 'POST':
        extra_context['form'] = TimeSlotForm(request.POST)
        if extra_context['form'].is_valid():
            ts = extra_context['form'].save(commit=False)
            ts.action = extra_context['action']
            ts.save()
            return HttpResponseRedirect(reverse('view_action', args=[extra_context['action'].id]))
    else:
        extra_context['form'] = TimeSlotForm()

    extra_context['form_description'] = "app_planning/descriptions/timeslot.html"
    extra_context['form_action'] = reverse('new_timeslot', args=[extra_context['action'].pk])
    return render_to_response(template, extra_context, context_instance=RequestContext(request))

def delete_timeslot(request, ts_id, template='', extra_context={}):
    try:
        ts = TimeSlot.objects.get(id=ts_id)
        ts.delete()
    except:
        pass
    
    return HttpResponseRedirect(reverse('view_action', args=[ts.action_id]))



